package ihm;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import moteur.Impl_Moteur;

import command.Coller;
import command.Command;
import command.Copier;
import command.Couper;
import command.InsererText;
import command.Supprimer;

@SuppressWarnings("serial")
public class Editeur extends JFrame implements ActionListener, CaretListener,
		KeyListener {

	private Command copier, couper, coller, insererText, supprimerKey,
			supprimer;

	private Impl_Moteur moteur;

	// private int debut, fin;

	/**************************
	 * GUI FIELDS
	 **************************/

	public JTextArea text;

	public JTextArea clipboard;

	private JPanel panelSouth, panelEast;

	/*
	 * MENUS
	 */
	private JMenuBar menuBar;
	private JMenu menuFile, menuEdit, menuHelp;
	private JMenuItem menuItemNew, menuItemExit, menuItemCut, menuItemCopy,
			menuItemPaste, menuItemdelete;

	public void initMenu() {
		menuBar = new JMenuBar();

		menuFile = new JMenu("File");
		menuEdit = new JMenu("Edit");
		menuHelp = new JMenu("About");

		menuItemNew = new JMenuItem("New");
		menuItemNew.addActionListener(this);

		menuItemExit = new JMenuItem("Exit");
		menuItemExit.addActionListener(this);

		menuItemCut = new JMenuItem("Cut");
		menuItemCut.addActionListener(this);
		menuItemCopy = new JMenuItem("Copy");
		menuItemCopy.addActionListener(this);
		menuItemPaste = new JMenuItem("Paste");
		menuItemdelete = new JMenuItem("Delete");
		menuItemdelete.addActionListener(this);
		menuItemPaste.addActionListener(this);

		menuFile.add(menuItemNew);
		menuFile.add(menuItemExit);
		menuBar.add(menuFile);

		menuEdit.add(new JSeparator());
		menuEdit.add(menuItemCut);
		menuEdit.add(menuItemCopy);
		menuEdit.add(menuItemPaste);
		menuEdit.add(menuItemdelete);

		menuBar.add(menuEdit);

		menuBar.add(menuHelp);

		this.getContentPane().add(menuBar, BorderLayout.NORTH);
	}

	public Editeur() {
		this.setTitle("Mini Editor  ");
		this.setLayout(new BorderLayout());
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		this.initMenu();

		text = new JTextArea();

		text.setPreferredSize(new Dimension(500, 300));
		text.setBounds(0, 0, text.getPreferredSize().width,
				text.getPreferredSize().height);
		text.addKeyListener(this);
		text.addCaretListener(this);
		this.getContentPane().add(text, BorderLayout.CENTER);

		/* Panel South */
		panelSouth = new JPanel(new FlowLayout());
		this.add(panelSouth, BorderLayout.SOUTH);

		this.getContentPane().setPreferredSize(text.getPreferredSize());
		this.setResizable(false);
		this.pack();
		this.setVisible(true);

		moteur = new Impl_Moteur(this);

		this.setCopierCommand(new Copier(moteur));
		this.setCouperCommand(new Couper(moteur));
		this.setCollerCommand(new Coller(moteur));
		this.setInsertTextCommand(new InsererText(moteur, this));
		this.setSupprimer(new Supprimer(moteur, this));
		// this.setSupprimerKey(new Supprimer_Key(moteur, this));

	}

	private void setSupprimer(Supprimer supprimer) {
		this.supprimer = supprimer;

	}

	// private void setSupprimerKey(Supprimer_Key supprimerKey) {
	// this.supprimerKey = supprimerKey;
	//
	// }

	public void setCopierCommand(Command copierCommand) {
		this.copier = copierCommand;
	}

	public void setCouperCommand(Command couperCommand) {
		this.couper = couperCommand;
	}

	public void setCollerCommand(Command collerCommand) {
		this.coller = collerCommand;
	}

	public void setInsertTextCommand(Command insertTextCommand) {
		this.insererText = insertTextCommand;
	}

	private char insertText;

	public char getCharInsere() {
		return insertText;
	}

	// public int getSelectionDebut() {
	// return debut;
	// }
	//
	// public int getSelectionFin() {
	// return fin;
	// }

	public void setText(String s) {
		this.text.setText(s);
	}

	/***************************
	 * handle events
	 ***************************/

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == menuItemCut) {
			couper.execute();
		} else if (e.getSource() == menuItemCopy) {
			copier.execute();
		} else if (e.getSource() == menuItemPaste) {
			coller.execute();
		} else if (e.getSource() == menuItemdelete) {
			supprimer.execute();
		}

	}

	@Override
	public void caretUpdate(CaretEvent e) {

		int debut = e.getDot();
		int fin = e.getMark();

		moteur.setSelection(debut, fin);

		// System.out.println("selection - editeur:" + moteur.getSelection());

	}

	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyTyped(KeyEvent e) {
		if ((int) e.getKeyChar() == KeyEvent.VK_DELETE) {

		} else if ((int) e.getKeyChar() == KeyEvent.VK_BACK_SPACE) {

			supprimer.execute();

		} else if (e.getKeyChar() >= 32 && e.getKeyChar() <= 126) {
			insertText = e.getKeyChar();
			if (insererText != null) {
				insererText.execute();
			}
		}
		// } else if (e.getKeyChar() == KeyEvent.VK_ENTER) {
		// int debut = moteur.getSelection().getDebut();
		// int fin = moteur.getSelection().getFin();
		//
		// moteur.setSelection(debut - 1, fin - 1);
		// System.out.println("position de selection debut--enter text :"
		// + moteur.getSelection().getDebut());
		// }

	}

	public static void main(String[] args) {
		Editeur window = new Editeur();
		window.setVisible(true);
	}

}
